package com.young.gpg.common.util;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.KeyPair;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 12/4/24 11:14 AM
 */
public class KeyGenUtil {

    public static void main(String[] args) {
        getKeyMap("163.com.cn");
    }


    public static Map<String,String> getKeyMap(String comment) {

        Map<String,String> keys = new HashMap<>();
        int type = KeyPair.RSA;
        JSch jsch = new JSch();

        try {
            KeyPair keyPair = KeyPair.genKeyPair(jsch, type, 2048);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            keyPair.writePrivateKey(outputStream);

            String privateKey = outputStream.toString();


            outputStream = new ByteArrayOutputStream();

            keyPair.writePublicKey(outputStream, comment);

            String publicKey = outputStream.toString();

            System.out.println("Finger print: " + keyPair.getFingerPrint());
            keyPair.dispose();


            System.out.println("-----------------------------");


            System.out.println(privateKey);

            keys.put("privateKey", privateKey);


            System.out.println("-----------------------------");

            System.out.println(publicKey);

            keys.put("publicKey", publicKey);



        } catch (JSchException e) {
            e.printStackTrace();
        }

        return keys;
    }
}
